package com.example.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.system.entity.Health;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface HealthMapper extends BaseMapper<Health> {
    @Select("SELECT * FROM health WHERE user_id = #{userId}")
    List<Health> findHealthByUserID(Integer userId);

    @Select("SELECT * FROM health WHERE user_id = #{userId} AND time BETWEEN #{beginTime} AND #{endTime}")
    List<Health> findHealthByUserIDAndTimeRange(@Param("userId") Integer userId,
                                                @Param("beginTime")LocalDateTime beginTime,
                                                @Param("endTime")LocalDateTime endTime);

    //@Insert("INSERT INTO health (health_id, user_id, heart_rate, blood_pressure, time) VALUES (#{healthId}, #{userId}, #{heartRate}, #{bloodPressure}, #{time})")
    void addHealthByHealth(Health health);

    @Select("SELECT COUNT(*) > 0 FROM health " +
            "WHERE user_id = #{userId} AND health_id = #{healthId};")
    boolean existsHealthKeyPair(@Param("userId") Integer userId,
                                   @Param("healthId") Integer healthId);

    @Delete("DELETE FROM health WHERE user_id = #{userId} AND time BETWEEN #{beginTime} AND #{endTime}")
    void deleteHealthByUserIdAndTimeRange(@Param("userId") Integer userId,
                                          @Param("beginTime")LocalDateTime beginTime,
                                          @Param("endTime")LocalDateTime endTime);

    @Delete("DELETE FROM health WHERE user_id = #{userId}")
    void deleteHealthByUserId(@Param("userId") Integer userId);
}
